package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.JwtClaimsConstant;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;


import com.sky.exception.PasswordEditFailedException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;


import com.sky.vo.EmployeeLoginVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Map;


@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

     /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
            password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    /**
     * 员工修改密码
     * @param passwordEditDTO
     */
    @Override
    public void updatePassword(PasswordEditDTO passwordEditDTO) {

        String oldPassword = passwordEditDTO.getOldPassword();
        String newPassword = passwordEditDTO.getNewPassword();


        oldPassword = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        newPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes());

        //根据id返回员工对象信息

        Long empId = BaseContext.getCurrentId();

        Employee employee = employeeMapper.getEmployeeById(empId);

        //判断旧密码是否对得上
        //对不上直接返回，报错两次密码错误
        //没有返回id
        if(!oldPassword.equals(employee.getPassword())){

            throw new PasswordEditFailedException(MessageConstant.PASSWORD_ERROR);

        }

        //对得上，将新密码加密后存入数据库
        passwordEditDTO.setNewPassword(newPassword);
        employeeMapper.updatePassword(newPassword,empId);

    }

    /**
     * 增加员工
     * @param employeeDTO
     */
    @Override
    public void addEmployee(EmployeeDTO employeeDTO) {
        //创建一个员工对象封装信息
        Employee employee = new Employee();
        //copy 数据到employee对象
        BeanUtils.copyProperties(employeeDTO,employee);
        //补充数据库字段，创建时间和更新时间
        //补充创建人和更新人 应用aop进行赋值

        //补充密码
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        //补充状态，默认是启动
        employee.setStatus(StatusConstant.ENABLE);


        employeeMapper.addEmployee(employee);
    }

    /**
     * 员工分页查寻
     * @param pagedot
     * @return
     */
    @Override
    public PageResult page(CategoryPageQueryDTO pagedot) {
        PageHelper.startPage(pagedot.getPage(),pagedot.getPageSize());

       // Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
        Page<Employee> employeePage= employeeMapper.page(pagedot.getName());

        return new PageResult(employeePage.getTotal(),employeePage.getResult());
    }

    /**
     * 修改员工回显
     * @param id
     * @return
     */
    @Override
    public Employee getByUsername(Long id) {

        Employee employee = employeeMapper.getEmployeeById(id);

        return employee;
    }

    /**
     * 修改员工信息
     * @param employeeDTO
     */
    @Override
    public void updateEmp(EmployeeDTO employeeDTO) {
        //创建一个员工对象封装信息
        Employee employee = new Employee();
        //copy 数据到employee对象
        BeanUtils.copyProperties(employeeDTO,employee);
        //修改更新时间

        employeeMapper.updateEmp(employee);
    }

    /**
     * 控制员工状态
     * @param status
     * @param id
     */
    @Override
    public void controlEmpStatus(Integer status, Long id) {
        //创建一个员工对象封装信息
        Employee employee = new Employee();
        //给对象赋值
        employee.setStatus(status);
        employee.setId(id);
        employeeMapper.controlEmpStatus(employee);
    }


}
